package com.atguigu.gmall.activity.receiver;

import com.atguigu.gmall.activity.model.UserRecode;
import com.atguigu.gmall.activity.service.SeckillBizService;
import com.atguigu.gmall.activity.service.SeckillGoodsService;
import com.atguigu.gmall.rabbit.config.MqConst;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.print.attribute.standard.PrinterURI;

/**
 * @author: atguigu
 * @create: 2023-09-22 14:32
 */
@Slf4j
@Component
public class ActivityReceiver {

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private SeckillBizService seckillBizService;

    /**
     * 监听定时任务发送秒杀预热消息；执行秒杀商品放入缓存
     *
     * @param msg
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_TASK, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_TASK_1, durable = "true"),
            key = MqConst.ROUTING_TASK_1
    ))
    public void processSeckill2Cache(String msg, Message message, Channel channel) {
        if (StringUtils.isNotBlank(msg)) {
            log.info("[秒杀服务]监听秒杀商品预热消息");
            seckillGoodsService.processSeckill2Cache();
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    /**
     * 监听秒杀队列中请求，完成秒杀核心业务处理
     *
     * @param userRecode
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_SECKILL_USER, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_SECKILL_USER, durable = "true"),
            key = MqConst.ROUTING_SECKILL_USER
    ))
    public void processSeckillRequest(UserRecode userRecode, Message message, Channel channel) {
        if (userRecode != null) {
            log.info("[秒杀服务]监听秒杀请求{}", userRecode);
            seckillBizService.processSeckillRequest(userRecode);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    /**
     * 监听扣减秒杀库存消息，更新Redis中库存数量，以及MySQL中库存数量
     *
     * @param userRecode
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_SECKILL_STOCK, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_SECKILL_STOCK, durable = "true"),
            key = MqConst.ROUTING_SECKILL_STOCK
    ))
    public void processStockDeduct(UserRecode userRecode, Message message, Channel channel) {
        if (userRecode != null) {
            log.info("[秒杀服务]监听扣减库存{}", userRecode);
            seckillBizService.processStockDeduct(userRecode);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    /**
     * 监听定时清理秒杀缓存消息
     *
     * @param msg
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_TASK, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_TASK_18, durable = "true"),
            key = MqConst.ROUTING_TASK_18
    ))
    public void processCleanCache(String msg, Message message, Channel channel){
        if (StringUtils.isNotBlank(msg)) {
            log.info("[秒杀服务]监听清理秒杀缓存消息：{}", msg);
            seckillBizService.processCleanCache();
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
